import * as XLSX from 'xlsx'
import XLSXStyle from 'xlsx-style'
import FileSaver from 'file-saver'

const setExlStyle = (data) => {
  const borderAll = { // 单元格外侧框线
    top: {
      style: 'thin'
    },
    bottom: {
      style: 'thin'
    },
    left: {
      style: 'thin'
    },
    right: {
      style: 'thin'
    }
  }

  data['!cols'] = []
  for (const key in data) {
    if (data[key] instanceof Object) {
      data[key].s = {
        border: borderAll,
        alignment: {
          horizontal: 'center', // 水平居中对齐
          vertical: 'center'
        },
        font: {
          sz: 11
        },
        bold: true,
        numFmt: 0
      }

      data['!cols'].push({ wch: 20 })
    }
  }
  return data
}
/*
* idSelectorArr ['id1', 'id2']
* filename  new Date().getTime()
*
*/
const ExportExcelData = (idSelectorArr, filename) => {
  var xlsxParam = {
    raw: true,
    cellDates: true
  }
  const workbook = XLSX.utils.book_new()
  const data = []
  // const ws1 = XLSX.utils.table_to_sheet(document.querySelector('#bjd_qdx'), xlsxParam)
  // const ws2 = XLSX.utils.table_to_sheet(document.querySelector('#bjd_bdx'), xlsxParam)
  // const ws3 = XLSX.utils.table_to_sheet(document.querySelector('#bjd_zjfy'), xlsxParam)
  // for (const i in idSelectorArr) {
  //   data[i] = XLSX.utils.table_to_sheet(document.querySelector('#' + i), xlsxParam)
  // }

  // console.log(idSelectorArr, filename, ' idSelectorArr, filename')

  // idSelectorArr = [
  //   {
  //     selector: XLSX.utils.table_to_sheet(document.querySelector('#bjd_qdx1'), xlsxParam),
  //     json: XLSX.utils.sheet_to_json(data[0][selector], { header: 1 })
  //   },
  //   {
  //     selector: XLSX.utils.table_to_sheet(document.querySelector('#bjd_qdx2'), xlsxParam),
  //     json: XLSX.utils.sheet_to_json(data[1][selector], { header: 1 })
  //   }
  // ]

  if (idSelectorArr.length) {
    idSelectorArr.map((selector, idx) => {
      data[idx] = { selector: '', json: '' }
      const t = XLSX.utils.table_to_sheet(document.querySelector('#' + selector), xlsxParam)
      // console.log(t, ' t')
      for (const i in t) {
        // console.log(t[i], ' t[i]')
        if (t[i].v) {
          if (!isNaN(Number(t[i].v))) {
            t[i].t = 'n'
            t[i].v = Number(t[i].v)
          }
        }
      }
      // console.log(t, ' t 222')
      data[idx].selector = t
      data[idx].json = XLSX.utils.sheet_to_json(t, { header: 1 })
    })
  } else {
    return
  }

  // console.log(data, ' data')

  // let a = XLSX.utils.sheet_to_json(ws1, { header: 1 })
  // const b = XLSX.utils.sheet_to_json(ws2, { header: 1 })
  // const c = XLSX.utils.sheet_to_json(ws3, { header: 1 })

  let json = []
  data.map((item, i) => {
    json = json.concat(item.json).concat([''])
  })
  // console.log(json, ' json')

  // a = a.concat(['']).concat(b).concat(['']).concat(c)
  // const worksheet = XLSX.utils.json_to_sheet(a, { skipHeader: true })
  const worksheet = XLSX.utils.json_to_sheet(json, { skipHeader: true })
  XLSX.utils.book_append_sheet(workbook, worksheet, filename)// sheetName 表名
  const wbData = workbook['Sheets'][filename]
  setExlStyle(wbData)
  const wbout = XLSXStyle.write(workbook, {
    type: 'buffer'
  })
  try {
    FileSaver.saveAs(
      new Blob([wbout], {
        type: 'application/octet-stream;charset=utf-8"'
      }), filename + '.xlsx')
  } catch (e) {
    if (typeof console !== 'undefined') console.log(e, wbout)
  }
  return wbout
}
const XLSXOBJ = {
  ExportExcelData
}
export {
  XLSXOBJ
}
